package com.example.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.Mapper.CgpgDao;
import com.example.pojo.Materialzhu;
import com.example.pojo.cgqg;
import com.example.pojo.cgqgcb;
import com.example.vo.cgqgcbvo;
import com.example.vo.cgqgvo;

@Service
@Transactional
public class CgpgBiz{
	@Autowired
	private CgpgDao daos;
	
	public CgpgDao getDaos() {
		return daos;
	}
	public void setDaos(CgpgDao daos) {
		this.daos = daos;
	}
	public void addcgpg(cgqg cg) {
		// TODO Auto-generated method stub
		Integer i= daos.selectqgidsfcz(cg.getBillno());
		if(i==0){
			daos.addcgqg(cg);
		}else{
			daos.delcgqg(cg.getBillno());
			daos.addcgqg(cg);
		}
		
	}

	public Integer selecCountBillno(){
		/*System.out.println("414141"+daos.selecCountBillno());*/
		return daos.selecCountBillno();
	}
	
	/**
	 * 根据单号查询
	 * @param billno
	 */
	public void delcgqg(String billno){
		daos.delcgqg(billno);
	}
	
	public cgqgvo selectcgpg(Integer pagenum) {
		// TODO Auto-generated method stub
		/*System.out.println("152347896"+daos.selectcgpg(pagenum));*/
		cgqg qg=daos.selectcgqg(pagenum);
		cgqgvo qgvo=new cgqgvo();
		qgvo.setBillno(qg.getBillno());
		qgvo.setBilldate(qg.getBilldate());
		qgvo.setBillstatus(qg.getBillstatus());
		qgvo.setBillstylename(qg.getBillstylename());
		qgvo.setCurcurrencyname(qg.getCurcurrencyname());
		qgvo.setDepartname(qg.getDepartname());
		qgvo.setFactmemo(qg.getFactmemo());
		qgvo.setMaker(qg.getMaker());
		qgvo.setPermitter(qg.getPermitter());
		qgvo.setSalesname(qg.getSalesname());
		qgvo.setShenhezhuangtai(qg.getShenhezhuangtai());
		qgvo.setZongjine(qg.getZongjine());
		List<cgqgcbvo> cgqgcbvos=new ArrayList<cgqgcbvo>();
		for (cgqgcb cgqgcb : qg.getQgcb()) {
			Materialzhu mtzhu=daos.selectmtzhubyid(cgqgcb.getPordid());
			 cgqgcbvo cbvo=new cgqgcbvo(cgqgcb.getHanghao(), cgqgcb.getBillno(),
					 cgqgcb.getPordid(), mtzhu.getProdName(), mtzhu.getProdsize(),
					 mtzhu.getDanwei(), mtzhu.getCurrName(), cgqgcb.getXuqiuriqi(),
					 cgqgcb.getJianyicaigouriqi(), cgqgcb.getShuliang(), 
					 cgqgcb.getWeicaigouliang(), cgqgcb.getFenlubeizhu(),
					 cgqgcb.getLaiyuandanhao(), cgqgcb.getLaiyuandanbie(), 
					 cgqgcb.getBiaozhunjinjia(), cgqgcb.getBiaozhunjinjiajine(), 
					 cgqgcb.getDanjia(), cgqgcb.getJinjiajine());
			 cgqgcbvos.add(cbvo); 
		}
		qgvo.setQgcbvo(cgqgcbvos);
		return qgvo;
	}
	
	/**
	 * 修改审核状态
	 * @param shenhezhuangtai
	 * @param billno
	 */
	public void updateqQGshenhe(String shenhezhuangtai,String billno){
		daos.updateqQGshenhe(shenhezhuangtai, billno);
	}

	/**
	 * 查询审核状态
	 * @param billno
	 * @return
	 */
	public String selectQgshzt(String billno){
		return daos.selectQgshzt(billno);
	}
	
	/**
	 * 查询所有请购
	 * @return
	 */
	public List<cgqg> queryAllcgqg(){
		return daos.queryAllcgqg();
	}
	/**
	 * 根据单号查询请购记录
	 * @param billno
	 * @return
	 */
	public cgqgvo querycgqgById(String billno){
		cgqg qg=daos.querycgqgById(billno);
		cgqgvo qgvo=new cgqgvo();
		qgvo.setBillno(qg.getBillno());
		qgvo.setBilldate(qg.getBilldate());
		qgvo.setBillstatus(qg.getBillstatus());
		qgvo.setBillstylename(qg.getBillstylename());
		qgvo.setCurcurrencyname(qg.getCurcurrencyname());
		qgvo.setDepartname(qg.getDepartname());
		qgvo.setFactmemo(qg.getFactmemo());
		qgvo.setMaker(qg.getMaker());
		qgvo.setPermitter(qg.getPermitter());
		qgvo.setSalesname(qg.getSalesname());
		qgvo.setShenhezhuangtai(qg.getShenhezhuangtai());
		qgvo.setZongjine(qg.getZongjine());
		List<cgqgcbvo> cgqgcbvos=new ArrayList<cgqgcbvo>();
		for (cgqgcb cgqgcb : qg.getQgcb()) {
			Materialzhu mtzhu=daos.selectmtzhubyid(cgqgcb.getPordid());
			 cgqgcbvo cbvo=new cgqgcbvo(cgqgcb.getHanghao(), cgqgcb.getBillno(),
					 cgqgcb.getPordid(), mtzhu.getProdName(), mtzhu.getProdsize(),
					 mtzhu.getDanwei(), mtzhu.getCurrName(), cgqgcb.getXuqiuriqi(),
					 cgqgcb.getJianyicaigouriqi(), cgqgcb.getShuliang(), 
					 cgqgcb.getWeicaigouliang(), cgqgcb.getFenlubeizhu(),
					 cgqgcb.getLaiyuandanhao(), cgqgcb.getLaiyuandanbie(), 
					 cgqgcb.getBiaozhunjinjia(), cgqgcb.getBiaozhunjinjiajine(), 
					 cgqgcb.getDanjia(), cgqgcb.getJinjiajine());
			 cgqgcbvos.add(cbvo); 
		}
		qgvo.setQgcbvo(cgqgcbvos);
		return qgvo;
	}
}
